//
//  DepoistInputInfoCell.swift
//  d11
//
//  Created by peale on 2018/10/21.
//  Copyright © 2018年 Tech_001. All rights reserved.
//

import UIKit
typealias InfoChangeBlock = (_ info: String,_ row:Int) -> Void
class DepoistInputInfoCell: UITableViewCell {

    var bgImgView : UIImageView?
    var iconImgView : UIImageView?
    var titleLabel : UILabel?
    var textField : UITextField?
    var subTitleLabel : UILabel?
    var copyBtn : UIButton?
    var lineView : UIView?
    var infoChangeBlock: InfoChangeBlock?
    func callChangeBlock(_ block: @escaping InfoChangeBlock) {
        
        infoChangeBlock = block
    }
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    func setUpUI() -> Void {
        self.backgroundColor  = UIColor.clear
        self.selectionStyle = UITableViewCell.SelectionStyle.none
        // self.contentView!.backgroundColor  = UIColor.clear
        self.bgImgView = UIImageView.init()
         self.bgImgView?.isUserInteractionEnabled = true
        self.bgImgView?.image = imageNamed("icon_my_cell_bottom")?.resizableImage(withCapInsets: UIEdgeInsets(top: 5, left: 30, bottom: 5, right: 30),resizingMode: .stretch) //左右15像素的部分不变，中间部分来拉伸
        self.contentView.addSubview(bgImgView!);
        self.bgImgView?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.height.equalTo(60)
            make.centerX.equalTo(self.contentView)
        };
        
        self.iconImgView = UIImageView.init()
        self.iconImgView?.image = imageNamed("icon_depoist_input_info_name")
        self.bgImgView?.addSubview(self.iconImgView!)
        self.iconImgView?.snp.makeConstraints{(make) in
            make.left.equalToSuperview().offset(12)
            make.width.equalTo(20)
            make.height.equalTo(20)
            make.centerY.equalTo(self.bgImgView!)
        }
        self.titleLabel = UILabel.init()
        self.titleLabel?.text = "null"
        self.titleLabel?.textColor = HexRGBAlpha(0x333333, 1)
        self.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        self.titleLabel?.textAlignment = NSTextAlignment.left
        self.bgImgView?.addSubview(self.titleLabel!)
        
        self.titleLabel?.snp.makeConstraints{(make) in
            make.left.equalTo((self.iconImgView?.snp.right)!).offset(12)
            make.width.equalTo(70)
            make.height.equalTo(51)
            make.centerY.equalToSuperview()
        }
        
        self.textField = UITextField.init()
        self.textField?.textColor =  HexRGBAlpha(0x8E8F90, 1)
        self.textField?.font = UIFont.systemFont(ofSize: 14)
        self.textField?.textAlignment = NSTextAlignment.left
     //   self.textField?.numberOfLines = 2
       // self.textField?.placeholder = "请输入存款人真实姓名"
        self.bgImgView?.addSubview(self.textField!)
        self.textField?.addTarget(self, action: #selector(self.actionToChanged(_:)), for: UIControl.Event.editingChanged)
        self.textField?.snp.makeConstraints{(make) in
            make.left.equalTo((self.titleLabel?.snp.right)!).offset(12)
            make.right.equalToSuperview().offset(-15)
            make.centerY.equalToSuperview()
            make.height.equalTo(51)
        }
        copyBtn = UIButton()
        copyBtn?.setTitle("复制", for: .normal)
        self.copyBtn?.setTitleColor(UIColor.init("#ff9900"), for: .normal)
        self.copyBtn?.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        self.copyBtn?.layer.borderColor = UIColor.init("#ff9900")?.cgColor
        self.copyBtn?.layer.cornerRadius = 2
        self.copyBtn?.layer.borderWidth = 1
        self.copyBtn?.isHidden = true
        self.copyBtn?.addTarget(self, action: #selector(copyText), for: .touchUpInside)
        self.bgImgView?.addSubview(copyBtn!)
        
        self.copyBtn?.snp.makeConstraints{(make) in
            make.right.equalToSuperview().offset(-15)
            make.centerY.equalToSuperview()
            make.height.equalTo(20)
            make.width.equalTo(40)
        }
        
        self.subTitleLabel = UILabel.init()
        self.subTitleLabel?.textColor =  HexRGBAlpha(0x8E8F90, 1)
        self.subTitleLabel?.font = UIFont.systemFont(ofSize: 14)
        self.subTitleLabel?.textAlignment = NSTextAlignment.left
        self.subTitleLabel?.numberOfLines = 2
        self.subTitleLabel?.isHidden = true
        // self.textField?.placeholder = "请输入存款人真实姓名"
        self.bgImgView?.addSubview(self.subTitleLabel!)
   //     self.textField?.addTarget(self, action: #selector(self.actionToChanged(_:)), for: UIControlEvents.editingChanged)
        self.subTitleLabel?.snp.makeConstraints{(make) in
            make.left.equalTo((self.titleLabel?.snp.right)!).offset(12)
            make.right.equalToSuperview().offset(-15)
            make.centerY.equalToSuperview()
            make.height.equalTo(51)
        }
        
        self.lineView = UIView.init()
        self.lineView?.backgroundColor = HexRGBAlpha(0xF3F3F3, 1)
        self.lineView?.isHidden = true
        self.bgImgView?.addSubview(self.lineView!)
        
        self.lineView?.snp.makeConstraints{(make) in
            make.centerX.equalToSuperview()
            make.width.equalToSuperview().offset(-20)
            make.height.equalTo(0.7)
            make.bottom.equalToSuperview()
        }
    }
    @objc func copyText(){
        UIPasteboard.general.string = self.subTitleLabel?.text
        UIHelper.alertSimpleMessage(about: "复制成功", withDescription: "")
    }
    func setData(inputText:String,row:Int,isBottom:Bool) -> Void {
        self.textField?.tag = row
        self.subTitleLabel?.isHidden = true
        self.textField?.isHidden = false
        if isBottom == true {
            self.lineView?.isHidden = true
            self.bgImgView?.image = imageNamed("icon_my_cell_bottom")?.resizableImage(withCapInsets: UIEdgeInsets(top: 5, left: 30, bottom: 5, right: 30),resizingMode: .stretch) //左右15像素的部分不变，中间部分来拉伸
        }else{
            self.lineView?.isHidden = false
            self.bgImgView?.image = imageNamed("icon_my_cell_top")?.resizableImage(withCapInsets: UIEdgeInsets(top: 5, left: 30, bottom: 5, right: 30),resizingMode: .stretch) //左右15像素的部分不变，中间部分来拉伸
        }
        if row == 0 {
            self.titleLabel?.text = "真实姓名"
            self.iconImgView?.image = imageNamed("icon_depoist_input_info_name")
            self.textField?.placeholder = "请输入存款人真实姓名"
             self.textField?.keyboardType  = .default
            self.textField?.text = inputText
        }else if row == 1{
            self.titleLabel?.text = "银行卡号"
            self.iconImgView?.image = imageNamed("icon_depoist_input_info_bankNo")
            self.textField?.placeholder = "请输入银行卡号"
             self.textField?.keyboardType  = .numberPad
              self.textField?.text = inputText
        }
    }
    func setData(inputText:String,row:Int)->Void{
        self.lineView?.isHidden = false
        self.textField?.isEnabled = false
        self.textField?.tag = row
        self.subTitleLabel?.isHidden = false
        self.textField?.isHidden = true
        self.bgImgView?.image = imageNamed("icon_my_cell_center")?.resizableImage(withCapInsets: UIEdgeInsets(top: 5, left: 30, bottom: 5, right: 30),resizingMode: .stretch) //左右15像素的部分不变，中间部分来拉伸
        if row == 1 {
            self.titleLabel?.text = "真实姓名"
            self.iconImgView?.image = imageNamed("icon_depoist_input_info_name")
            let text = inputText.replacingOccurrences(of: "-", with: "\n")
            self.subTitleLabel?.text = text
            self.copyBtn?.isHidden = false
        }else if row == 2{
            self.titleLabel?.text = "银行卡号"
            self.iconImgView?.image = imageNamed("icon_depoist_input_info_bankNo")
            let text = inputText.replacingOccurrences(of: "-", with: "\n")
            self.subTitleLabel?.text = text
            self.copyBtn?.isHidden = false
        }else if row == 3{
            self.titleLabel?.text = "充值金额"
            self.iconImgView?.image = imageNamed("icon_depoist_money_icon")
            self.subTitleLabel?.text = inputText
        }
    }
    
    
    @objc func actionToChanged(_ sender: UITextField) -> Void {
        if infoChangeBlock != nil {
            infoChangeBlock!(sender.text!,sender.tag)
        }
    }
}
